﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Helpers;
using System.Data;

namespace SYcwpb.Views
{
    /// <summary>
    /// NewStatisticSetting.xaml 的交互逻辑
    /// </summary>
    public partial class NewStatisticSetting : Window
    {
        public static NewStatisticSetting _view = null;
        public static NewStatisticSetting GetInstance()
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
                _view = new NewStatisticSetting();

            return _view;
        }

        private NewStatisticSetting()
        {
            InitializeComponent();
        }

        private List<Station> list = new List<Station>();
        SqlDataLayer sqlHelper = new SqlDataLayer();
        Dictionary<int, string> StationID_To_Name;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (CONSTDATA.UserModel.Line == "1")
            {
                StationID_To_Name = CONSTDATA.InitStationID_To_Name_1();
            }
            else
            {
                StationID_To_Name = CONSTDATA.InitStationID_To_Name_2();
            }

            foreach (var item in StationID_To_Name)
            {
                Station model = new Station();
                model.ID = item.Key;
                model.StationName = item.Value;
                list.Add(model);
            }
            StarStation.ItemsSource = list;
            StarStation.DisplayMemberPath = "StationName";
            StarStation.SelectedValuePath = "ID";

            EndStation.ItemsSource = list;
            EndStation.DisplayMemberPath = "StationName";
            EndStation.SelectedValuePath = "ID";
            GetKilometersItemsSource();
            GetKilometersOutAndIn();
        }

        public void GetKilometersOutAndIn()
        {
            string str = "select * from NewOutAndInKilometers where LineID='" + CONSTDATA.UserModel.Line + "'";
            DataTable dt = sqlHelper.SelectDt(str);
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("缺失数据，请联系管理员");
                return;
            }
            string gonglishu1 = dt.Rows[0]["OutKilometers"].ToString();
            string gonglishu2 = dt.Rows[0]["InKilometers"].ToString();
            gongli1.Text = gonglishu1;
            gongli2.Text = gonglishu2;
        }

        public void GetKilometersItemsSource()
        {
            string str = "select * from NewKilometers where LineID='" + CONSTDATA.UserModel.Line + "'";
            DataTable dt = sqlHelper.SelectDt(str);
            List<KilometersList> Klist = GetKilometersList(dt);
            dgKilometer.ItemsSource = Klist;
        }

        private void dgKilometer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgKilometer.SelectedItem == null)
                return;
            KilometersList model = dgKilometer.SelectedItem as KilometersList;
            gongli.Text = model.Kilometer.ToString();
        }

        private List<KilometersList> GetKilometersList(DataTable dt)
        {
            List<KilometersList> list = new List<KilometersList>();
            foreach (DataRow row in dt.Rows)
            {
                KilometersList model = new KilometersList();
                model.ID = int.Parse(row["ID"].ToString());
                model.StartStation = StationID_To_Name[int.Parse(row["StartStationID"].ToString())];
                model.EndStation = StationID_To_Name[int.Parse(row["EndStationID"].ToString())];
                model.LineID = int.Parse(row["LineID"].ToString());
                model.Kilometer = double.Parse(row["Kilometers"].ToString());
                list.Add(model);
            }
            return list;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (dgKilometer.SelectedItem == null)
            {
                MessageBox.Show("请选择需要修改的公里数");
                return;
            }
            if (string.IsNullOrEmpty(gongli.Text))
            {
                MessageBox.Show("公里数不能为空");
                return;
            }
            double a = 0;
            if (!double.TryParse(gongli.Text, out a))
            {
                MessageBox.Show("公里数必须为数字");
                return;
            }
            KilometersList model = dgKilometer.SelectedItem as KilometersList;
            string str = "update NewKilometers set Kilometers='" + gongli.Text + "' where ID='" + model.ID + "'";
            sqlHelper.ExecuteSql(str);
            MessageBox.Show("修改成功");
            GetKilometersItemsSource();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (dgKilometer.SelectedItem == null)
            {
                MessageBox.Show("请选择需要删除的公里数");
                return;
            }
            KilometersList model = dgKilometer.SelectedItem as KilometersList;
            string str = "delete from NewKilometers where ID='" + model.ID + "'";
            sqlHelper.ExecuteSql(str);
            MessageBox.Show("删除成功");
            gongli.Text = "";
            GetKilometersItemsSource();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (StarStation.SelectedValue == null)
            {
                MessageBox.Show("开始站点不能为空");
                return;
            }
            if (EndStation.SelectedValue == null)
            {
                MessageBox.Show("结束站点不能为空");
                return;
            }
            if (string.IsNullOrEmpty(gongli.Text))
            {
                MessageBox.Show("公里数不能为空");
                return;
            }
            double a = 0;
            if (!double.TryParse(gongli.Text,out a))
            {
                MessageBox.Show("公里数必须为数字");
                return;
            }
            string str = "insert into NewKilometers (StartStationID,EndStationID,Kilometers,LineID) values ('" + StarStation.SelectedValue + "','" + EndStation.SelectedValue + "','" + gongli.Text + "','" + CONSTDATA.UserModel.Line + "')";
            sqlHelper.ExecuteSql(str);
            MessageBox.Show("添加成功");
            GetKilometersItemsSource();
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            double a = 0;
            if (string.IsNullOrEmpty(gongli1.Text))
            {
                MessageBox.Show("出段公里数不能为空");
                return;
            }
            if (!double.TryParse(gongli1.Text, out a))
            {
                MessageBox.Show("出段公里数必须为数字");
                return;
            }
            if (string.IsNullOrEmpty(gongli2.Text))
            {
                MessageBox.Show("入段公里数不能为空");
                return;
            }
            if (!double.TryParse(gongli2.Text, out a))
            {
                MessageBox.Show("入段公里数必须为数字");
                return;
            }
            string str = "update NewOutAndInKilometers set OutKilometers='" + gongli1.Text + "',InKilometers='" + gongli2.Text + "' where LineID='" + CONSTDATA.UserModel.Line + "'";
            sqlHelper.ExecuteSql(str);
            MessageBox.Show("修改成功");
            GetKilometersOutAndIn();
        }
    }

    class Station
    {
        public int ID { get; set; }
        public string StationName { get; set; }
    }

    class KilometersList
    {
        public int ID { get; set; }
        public string StartStation { get; set; }
        public string EndStation { get; set; }
        public int LineID { get; set; }
        public double Kilometer { get; set; }
    }
}
